package com.example.demo.service.impl;

import com.example.demo.dao.UserMapper;
import com.example.demo.pojo.po.User;
import com.example.demo.pojo.vo.AddUser;
import com.example.demo.pojo.vo.PageResult;
import com.example.demo.pojo.vo.QueryPageBean;
import com.example.demo.pojo.vo.UpdateUser;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public  class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper mapper;

    public PageResult findByPage(QueryPageBean queryPageBean){
        List<User> list = mapper.findUserByPage(queryPageBean);
        Long total = mapper.findUserTotal(queryPageBean);

        PageResult pageResult = new PageResult(total, list);
        return pageResult;
    }


    public void addUser(AddUser au) {
        mapper.addUser(au);
        mapper.addRoleByUid(au.getId(),au.getRoleIds());
    }


    public void updateUser(UpdateUser uu) {
        mapper.updateUser(uu);
        mapper.deleteRoleByUid(uu.getId());
        mapper.addRoleByUid(uu.getId(),uu.getRoleIds());
    }


    public void deleteUser(String uidStr) {
        int uid = Integer.parseInt(uidStr);
        mapper.deleteUserById(uid);
        mapper.deleteRoleByUid(uid);
    }


}
